

import java.io.Serializable;
import java.net.InetAddress;

public class User implements Serializable {
	/**
	 * The serial ID for this class.
	 */
	private static final long serialVersionUID = -6200677381258858901L;
	/**
	 * The username used by the user represented by this object.
	 */
	private String username;
	/**
	 * The number of cryptochess wins this user has.
	 */
	private int wins;
	/**
	 * The number of cryptochess losses this user has.
	 */
	private int losses;
	/**
	 * The number of cryptochess draws this user has.
	 */
	private int draws;
	/**
	 * The elo rating of this user, calculated by using the algorithm found on
	 * http://www.chesselo.com/.
	 */
	private int rating;
	/**
	 * The enum value for the icon used by the user.
	 */
	private UserIcon icon;
	/**
	 * The ip address associated with this user. This is an optional field.
	 */
	private InetAddress ip;
	/**
	 * The password for the user, only set when adding a user to the database
	 */
	private String password;
	/**
	 * Color of the pieces the user should use. Used to tell opponent their color.
	 */
	private Color playerColor;
	
	public User(String u) {
		username = u;
		wins = 0;
		losses = 0;
		rating = 1000;
		icon = UserIcon.pawn_white;
	}
	
	public User(String u, int w, int l, int d, int r, UserIcon i) {
		username = u;
		wins = w;
		losses = l;
		draws = d;
		rating = r;
		icon = i;
	}
	
	public String getUsername() {
		return username;
	}
	
	public int getWins() {
		return wins;
	}
	
	public int getLosses() {
		return losses;
	}
	
	public int getDraws() {
		return draws;
	}
	
	public int getRating() {
		return rating;
	}
	
	public void setIpAddress(InetAddress addr) {
		ip = addr;
	}
	
	public InetAddress getIpAddress() {
		return ip;
	}
	
	public void setUserIcon(UserIcon u) {
		icon = u;
	}
	
	public UserIcon getUserIcon() {
		return icon;
	}
	
	public void setPassword(String pass) {
		password = pass;
	}
	
	public String getPassword() {
		return password;
	}
	
	public void setColor(Color c) {
		playerColor = c;
	}
	
	public Color getColor() {
		return playerColor;
	}
	
	@Override
	public String toString() {
		return username + ":" + wins + ":" + losses + ":" + draws + ":" + rating + ":" + icon.name() + ":";
	}
	
	@Override
	public boolean equals(Object o) {
		if(o.getClass().equals(this.getClass())) {
			User u = (User)o;
			if(u.username.equals(this.username)) {
				return true;
			}
		}
		return false;
	}
	
	public static User parseUser(String user) {
		User result = null;
		String[] arr = user.split(":");
		
		if(arr.length == 6) {
			result = new User(arr[0], 
							  Integer.parseInt(arr[1]), 
							  Integer.parseInt(arr[2]), 
							  Integer.parseInt(arr[3]), 
							  Integer.parseInt(arr[4]),
							  UserIcon.valueOf(arr[5]));
		}
		
		return result;
	}
	
	/*public void recordResult(GameResult result, User opponent) {
		int ratingDiff = Math.abs(rating - opponent.getRating());
		double expectedResult = 0.00;
		double actualResult = 0.00;
		int kfactor = 10;
		
		// Record the changes in the win-loss record
		switch(result) {
		case win:
			wins++;
			actualResult = 1.00;
			break;
		case draw:
			draws++;
			actualResult = 0.50;
			break;
		case loss:
			losses++;
			actualResult = 0.00;
			break;
		}
		
		// Calculate the new elo rating
		if(rating > opponent.getRating()) {
			if(ratingDiff > 735) {
				expectedResult = 1.00;
			} else if (ratingDiff > 500) {
				expectedResult = 0.99;
			} else if (ratingDiff > 450) {
				expectedResult = 0.96;
			} else if (ratingDiff > 400) {
				expectedResult = 0.94;
			} else if (ratingDiff > 350) {
				expectedResult = 0.92;
			} else if (ratingDiff > 300) {
				expectedResult = 0.89;
			} else if (ratingDiff > 250) {
				expectedResult = 0.85;
			} else if (ratingDiff > 200) {
				expectedResult = 0.81;
			} else if (ratingDiff > 150) {
				expectedResult = 0.76;
			} else if (ratingDiff > 100) {
				expectedResult = 0.70;
			} else if (ratingDiff > 50) {
				expectedResult = 0.64;
			} else if (ratingDiff > 25) {
				expectedResult = 0.57;
			} else {
				expectedResult = 0.53;
			}
		} else {
			if(ratingDiff > 735) {
				expectedResult = 0.00;
			} else if (ratingDiff > 500) {
				expectedResult = 0.01;
			} else if (ratingDiff > 450) {
				expectedResult = 0.04;
			} else if (ratingDiff > 400) {
				expectedResult = 0.06;
			} else if (ratingDiff > 350) {
				expectedResult = 0.08;
			} else if (ratingDiff > 300) {
				expectedResult = 0.11;
			} else if (ratingDiff > 250) {
				expectedResult = 0.15;
			} else if (ratingDiff > 200) {
				expectedResult = 0.19;
			} else if (ratingDiff > 150) {
				expectedResult = 0.24;
			} else if (ratingDiff > 100) {
				expectedResult = 0.30;
			} else if (ratingDiff > 50) {
				expectedResult = 0.36;
			} else if (ratingDiff > 25) {
				expectedResult = 0.43;
			} else if (ratingDiff > 0 ) {
				expectedResult = 0.47;
			} else {
				expectedResult = 0.50;
			}
		}
		
		if((wins + draws + losses) > 30) {
			kfactor = 10;
		} else if((wins + draws + losses) > 20) {
			kfactor = 20;
		} else {
			kfactor = 30;
		}
		
		rating = (int)((double)kfactor * (actualResult - expectedResult));
	}*/
}
